﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Yeap.Config;

namespace Yeap.Kernel.Storage
{
    public class Professor
    {
        public Int32 Id { get; set; }
        public String Login { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Rank { get; set; }
        public Dictionary<String, String> Properties { get; set; }

        public String FullName
        {
            get { return LastName + " " + FirstName; }
        }

        private readonly ISiteRepository siteRepository = new SiteRepository();

        public Dictionary<Int32, Int32> CoursesNorm
        {
            get
            {
                var norms =  new Dictionary<int, int>();
                Int32 minimalCourse, maximalCourse;
                try
                {
                    minimalCourse = Convert.ToInt32(siteRepository.GetSetting(Constants.SiteSettingKeys.MinimalCourse));
                    maximalCourse = Convert.ToInt32(siteRepository.GetSetting(Constants.SiteSettingKeys.MaximalCourse));
                }
                catch (FormatException)
                {
                    throw  new Exception(Constants.ExceptionMessages.InvalidSiteSettingIntValue);
                }
                catch(OverflowException)
                {
                    throw new Exception(Constants.ExceptionMessages.SiteSettingOverflow);
                }
                
                for (int course = minimalCourse; course <= maximalCourse; course++)
                {
                    if (Properties.ContainsKey(String.Format(Constants.UserSettingKeys.CourseNormPattern, course)))
                    {
                        try
                        {
                            norms.Add(course, Convert.ToInt32(Properties[String.Format(Constants.UserSettingKeys.CourseNormPattern, course)]));
                        }
                        catch (FormatException)
                        {
                            throw new Exception(Constants.ExceptionMessages.InvalidUserSettingIntValue);
                        }
                        catch(OverflowException)
                        {
                            throw new Exception(Constants.ExceptionMessages.UserSettingOverflow);
                        }
                    }
                    else
                    {
                        norms.Add(course, 0);
                    }
                }

                return norms;
            }
        }
    }
}